package com.javaProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javaProject.dto.R;
import com.javaProject.entity.Student;
import com.javaProject.entity.Teacher;
import com.javaProject.mapper.StudentMapper;
import com.javaProject.service.TeacherService;
import com.javaProject.mapper.TeacherMapper;
import com.javaProject.service.UserkeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;
import java.util.Objects;

/**
* @author ASUS
* @description 针对表【teacher】的数据库操作Service实现
* @createDate 2024-03-08 11:07:54
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService{

    @Autowired
    SM2 sm2;

    @Autowired
    UserkeyService keyService;

    @Autowired
    StudentMapper studentMapper;

    @Override
    public R login(Teacher teacher) throws NoSuchAlgorithmException, InvalidKeySpecException {
        QueryWrapper<Teacher> query= Wrappers.query(Teacher.class);
        query.eq("username",teacher.getUsername());
        Teacher one = getOne(query);
        if (Objects.isNull(one)){
            return R.fail().setMsg("用户不存在").setCode(-2);
        }
        String password = sm2.decrypt(one.getPassword(),keyService.getPrivateKey(teacher.getUsername()));
        System.out.println(password);
        one.setPassword(password);
        if(Objects.equals(teacher.getPassword(),password)){
            return R.ok().setData(one);
        }
        return R.fail().setMsg("密码错误").setCode(-1);
    }

    @Override
    public R add(Teacher teacher) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
        QueryWrapper<Teacher> query = Wrappers.query(Teacher.class);
        query.eq("username",teacher.getUsername());
        KeyPair keyPair = sm2.generateKeyPair();
        System.out.println(keyPair.getPublic());
        String pasword = sm2.encrypt(teacher.getPassword(),keyPair.getPublic());
        keyService.saveKey(teacher.getUsername(),keyPair.getPrivate());
        teacher.setPassword(pasword);
        Teacher one = getOne((query));
        if (Objects.nonNull(one)){
            return R.fail().setMsg("该用户名已注册").setCode(-1);
        }
        Student student = studentMapper.selectOne(Wrappers.<Student>query().eq("username",teacher.getUsername()));
        if (Objects.nonNull(student)){
            return R.fail().setMsg("该用户名已被使用").setCode(-1);
        }
        if (save(teacher)){
            return R.ok().setMsg("用户注册成功").setData(teacher);
        }
        return R.fail().setMsg("注册失败");

    }

    @Override
    public R upDate(Teacher teacher) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
        QueryWrapper<Teacher> query = Wrappers.query(Teacher.class);
        query.eq("username",teacher.getUsername());
        KeyPair keyPair = sm2.generateKeyPair();
        System.out.println(keyPair.getPublic());
        String pasword = sm2.encrypt(teacher.getPassword(),keyPair.getPublic());
        keyService.saveKey(teacher.getUsername(),keyPair.getPrivate());
        teacher.setPassword(pasword);
        Teacher one = getOne((query));
        if (Objects.isNull(one)){
            return R.fail().setMsg("用户不存在").setCode(-2);
        }
        boolean updateResult = update(teacher, query);

        if (updateResult) {
            return R.ok().setMsg("用户信息修改成功").setData(teacher);
        }
        return R.fail().setMsg("用户信息修改失败");
    }

    @Override
    public List<Teacher> getAllTeachers() throws NoSuchAlgorithmException, InvalidKeySpecException {
        QueryWrapper<Teacher> query = Wrappers.query(Teacher.class);
        List<Teacher> teachers = list(query);
        for (Teacher teacher : teachers) {
            String password = sm2.decrypt(teacher.getPassword(),keyService.getPrivateKey(teacher.getUsername()));
            teacher.setPassword(password);
        }
        return  teachers;
    }

    @Override
    public R Delect(Teacher teacher) {
        QueryWrapper<Teacher> query = Wrappers.query(Teacher.class);
        query.eq("username",teacher.getUsername());
        Teacher one = getOne(query);
        if (Objects.isNull(one)){
            return R.fail().setMsg("用户不存在").setCode(-2);
        }
        if (remove(query)){
            return R.ok().setMsg("用户删除成功");
        }
        return R.fail().setMsg("用户信息删除失败");
    }

    @Override
    public R getTeacher(Teacher teacher) throws NoSuchAlgorithmException, InvalidKeySpecException {
        QueryWrapper<Teacher> query = Wrappers.query(Teacher.class);
        query.eq("username",teacher.getUsername());
        Teacher one = getOne(query);
        if (Objects.isNull(one)){
            return R.fail().setMsg("用户不存在").setCode(-2);
        }
        String password = sm2.decrypt(one.getPassword(),keyService.getPrivateKey(teacher.getUsername()));
        System.out.println(password);
        one.setPassword(password);
            return R.ok().setData(one);
    }


}




